package com.item.action;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.base.Page;
import com.base.dialect.PaginationSupport;
import com.base.util.DateUtil;
import com.base.util.FileExportUtils;
import com.item.dao.model.EssenceNew;
import com.item.dao.model.EssenceReadStateExample;
import com.item.dao.model.MediaFile;
import com.item.dao.model.MediaFileExample;
import com.item.dao.model.MediaFileExample.Criteria;
import com.item.exception.JSONRepUtils;
import com.item.service.EssenceNewService;
import com.item.service.MediaFileService;
import com.item.util.ConstantResources;
import com.item.util.MyFileUtil;
import com.item.util.MyFileExportUtils;
import com.item.util.MyOssClient;
import com.item.util.SubjectUtils;

@Controller
public class MediaFileController {
	@Autowired
	private MediaFileService mediaService;
	
	@Autowired
	private EssenceNewService essenceService;
	
	@RequestMapping("/listMediaFile")
	@ResponseBody
	public String list(Page param,MediaFile media) throws Exception{
		PaginationSupport.setContext(param.getPageStart(), param.getPageSize(), null);
		MediaFile check = new MediaFile();
		//以下两个字段用来做筛选  分别是title和source的模糊匹配
		if(StringUtils.isNotBlank(media.getFilePath())){
			check.setFilePath("%"+media.getFilePath()+"%");
		}
		if(StringUtils.isNotBlank(media.getName())){
			check.setName("%"+media.getName()+"%");
		}
		List<MediaFile> list = mediaService.getMediaFIlesByName(check);
		Iterator<MediaFile> itr = list.iterator();
		while(itr.hasNext()){
			MediaFile file = itr.next();
			if(file.getFileSize() > 0){
				file.setFileBelong(MyFileUtil.getFileExt(file.getFileName()));
				file.setEssenceId(MyFileUtil.getFileSize(file.getFileSize()));
			}else{
				itr.remove();
			}
		}
		return JSONRepUtils.serialize(new Page(PaginationSupport.getContext().getTotalCount(), list), 0);
	}
	
	@RequestMapping("/showlistMediaFile")
	public String list() {
		return "mediaFile/mediaFileList";
	}
	
	@RequestMapping("/toEditMediaFile")
	public String toEdit()throws Exception{
		return "mediaFile/mediaFileEdit";
	}
	
	@RequestMapping(value="/saveMediaFile",method=RequestMethod.POST)
	@ResponseBody
	public String save(MediaFile media, HttpSession session)throws Exception{
		try{
			if(StringUtils.isBlank(media.getFileId())){
			}else{
				mediaService.updateByPrimaryKeySelective(media);
			}
		}catch(Exception e){
			e.printStackTrace();
			return "{\"success\":false}";
		}
		
		return "{\"success\":true}";
	}
	
	@RequestMapping(value="/findByIdMediaFile")
	@ResponseBody
	public String findById(String id,HttpSession session)throws Exception{	
		MediaFile n = mediaService.selectByPrimaryKey(id);
		return JSONRepUtils.serialize(n, 0);
	}
	
	/**
	 * type为0表示只删除条目，为1表示连文件一起删除
	 * @param id
	 * @param type
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/delMediaFile")
	@ResponseBody
	public String del(String id,Integer type)throws Exception{
		return delete(id, type);
	}
	
	public String delete(String id, Integer type){
		try{
			String[] ids = id.split(",");
			for(String fileId : ids){
				if(type == 1){
					MediaFile file = mediaService.selectByPrimaryKey(fileId);
					MyOssClient.deleteObject(file.getFilePath(), ConstantResources.BUCKET_NAME_WEBFILE);
				}
				mediaService.deleteByPrimaryKey(fileId);
			}
		}catch(Exception e){
			return "{\"success\":false}";
		}
		return "{\"success\":true}";
	}
	
	@RequestMapping("/topSetMediaFile")
	@ResponseBody
	public String topSet(String id,HttpSession session) throws Exception {
		return "{\"success\":true}";
	}
	
	@RequestMapping("/topDelMediaFile")
	@ResponseBody
	public String topDel(String id,HttpSession session) throws Exception {
		return "{\"success\":true}";
	}
}

